package cays.music;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import javax.print.Doc;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;

/**
 * 爬取网易云音乐
 *
 * @author Chai yansheng
 * @create 2019/8/24
 **/
public class NeteaseCloudMusicDemo {
    public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
            "(KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
    /**
     * 获取网站document
     * @param url
     * @return
     */
    public Document getDocument(String url) {
        Document document = null;
        try {
            document = Jsoup.connect(url)
                    .header("User-Agent",USER_AGENT)
                    .timeout(3000).ignoreContentType(true).get();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }
    // http://music.163.com/song/media/outer/url?id=436514312.song
    // http://music.163.com/song/media/outer/url?id=167876.song
    // http://music.163.com/song/media/outer/url?id=1327456179.song
    /**
     * 执行下载
     * @param url
     */
    public void execute(String url) {
        Document document = getDocument(url);
        //System.out.println(document.body());
        Elements albums = document.select("div.u-cover.u-cover-alb3 a");
        System.out.println("albums size : " + albums.size());
        Set<String> albumUrls = new HashSet<>();
        albums.forEach(song -> {
            String albumUrl = song.absUrl("href");
            if (!albumUrl.isEmpty()) {
                albumUrls.add(albumUrl);
                System.out.println(albumUrl);
            }
        });
        Set<String> songUrls = new HashSet<>();
        albumUrls.forEach(albumUrl -> {
            Document document1 = getDocument(albumUrl);
            // 歌曲id
            Elements songs = document1.select("ul.f-hide a");
            songs.forEach(song -> {
                String songUrl = song.absUrl("href");
                if (!songUrl.isEmpty()) {
                    String id = getIdOfUrl(songUrl);
                    // 歌曲下载地址
                    String songDownloadUrl = "http://music.163.com/song/media/outer/url?id=" + id + ".mp3";
                    songUrls.add(songDownloadUrl);
                    System.out.println("song download url : " + songDownloadUrl);
                }
            });
        });
        System.out.println("song size : " + songUrls.size());
        songUrls.forEach(songUrl -> {
            downloadSong(songUrl);
        });
    }

    /**
     * 获取url中的 id 参数的值
     * @param url
     * @return
     */
    public String getIdOfUrl(String url) {
        String []baseUrlAndParams = url.split("\\?");
        if (baseUrlAndParams.length != 2) {
            return "";
        }
        String []params = baseUrlAndParams[1].split("&");
        if (params.length <= 0) {
            return "";
        }
        for (String param : params) {
            if (param.contains("id=")) {
                return param.split("=")[1];
            }
        }
        return "";
    }
    /**
     * 将song到本地文件夹
     * @param songUrl
     */
    public void downloadSong(String songUrl) {
        // 若指定文件夹没有，则先创建
        String filePath = "src\\main\\java\\cays\\music\\mu_li\\";
        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        // 设置song文件名
        String fileName = getIdOfUrl(songUrl);
        // 写出的路径
        File file = new File(filePath + File.separator + fileName);

        try {
            // 获取songURL
            URL url = new URL(songUrl);
            // 获得连接
            URLConnection connection = url.openConnection();
            // 设置10秒的相应时间
            connection.setConnectTimeout(10 * 1000);
            // 获得输入流
            InputStream in = connection.getInputStream();
            // 获得输出流
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
            // 构建缓冲区
            byte[] buf = new byte[1024];
            int size;
            // 写入到文件
            while (-1 != (size = in.read(buf))) {
                out.write(buf, 0, size);
            }
            out.close();
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        NeteaseCloudMusicDemo musicDemo = new NeteaseCloudMusicDemo();
        // 歌手id
        String id = "4292";
        // 歌手界面
        String url = "https://music.163.com/artist/album?id=" + id + "&limit=120&offset=0";
        musicDemo.execute(url);
    }
}
